package com.yanqu.road.server.cmd.strongholdwar.eliminate;

import com.yanqu.road.entity.activity.stronghold.StrongholdEliminateReport;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_ROSS_STRONGHOLD_ELIMINATE_REPORT, desc = "")
public class CrossGetEliminateReportCmd extends CrossUnionCommand {
    private int activityId;
    private int id;
    private int type;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.CrossGetEliminateUnionReportReqMsg req = StrongholdWarProto.CrossGetEliminateUnionReportReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        id = req.getId();
        type = req.getType();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        Map<String, StrongholdEliminateReport> reportMap = CrossStrongholdMgr.getEliminateReport(activityId, groupId, id, type);
        StrongholdWarProto.GetEliminateUnionReportRespMsg.Builder resp = StrongholdWarProto.GetEliminateUnionReportRespMsg.newBuilder();
        List<StrongholdEliminateReport> list = new ArrayList<>(reportMap.values());
        list.sort((o1, o2) -> {
            int ret = o2.getDamageRate() - o1.getDamageRate();
            if (ret == 0) {
                return o2.getTotalDamage().compareTo(o1.getTotalDamage());
            }
            return ret;

        });
        for (StrongholdEliminateReport report : list) {
            StrongholdWarProto.StrongholdEliminateUnionReportDataMsg.Builder msg = StrongholdWarProto.StrongholdEliminateUnionReportDataMsg.newBuilder();
            msg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(report.getUnionBaseInfo()));
            msg.setDamageRate(report.getDamageRate());
            resp.addReport(msg);
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_STRONGHOLD_WAR_ELIMINATION_REPORT, resp);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
